<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Testimport extends MY_Luckydog
{
	public function __construct()
	{
		parent::__construct();
        $login_status = $this->login_status = $this->check_login_status();
        $this->load->model("luckydog/Testimport_model",'test');
        if (empty($login_status)){
            header("Location:login");
        }
	}
 

    public function index(){
        $_SESSION['v_menu'] = 'testimport';
        $this->load->view("/luckydog/import/testimport");
    }
    public function fenxi()
    {
        if(empty($_FILES)) header("Location:index");
        require_once(APPPATH.'libraries/PHPExcel.php');
        require_once(APPPATH.'libraries/PHPExcel/IOFactory.php');
        require_once(APPPATH.'libraries/PHPExcel/Reader/Excel5.php');
        
        $showmessage = '';
        $tmp_nameArr = $_FILES["fileData"]['tmp_name'];
        foreach ($tmp_nameArr as $key => $tmp_name) {
            $filename = $_FILES["fileData"]['name'][$key];
            $filenamesuffix = substr($filename,stripos($filename,".")+1);
            if(in_array($filenamesuffix,['xls','xlsx'])){
                $result = $this->analyseExcel($tmp_name);
                if($result)
                {
                   $showmessage .= $_FILES["fileData"]['name'][$key]."--<span style='color:green;'>成功</span><br />";
                }else{
                    $showmessage .= $_FILES["fileData"]['name'][$key]."--<span style='color:red;'>失败</span><br />";
                }
            }else{
                $showmessage .= "所有数据导入失败";
            }
        }
        $data = ['message'=>$showmessage];
        $this->load->view("/luckydog/import/testimport",$data);
    }
    /**
     * 分析excel数据
     * @return [type] [description]
     */
    public function analyseExcel($tmp_name){
        $objReader = PHPExcel_IOFactory::createReader('Excel2007');//use Excel2007 for 2007 format 
        $objPHPExcel = $objReader->load($tmp_name); //$filename可以是上传的文件，或者是指定的文件
        $sheet = $objPHPExcel->getSheet(0); 
        $highestRow = $sheet->getHighestRow(); // 取得总行数 
        $highestColumn = $sheet->getHighestColumn(); // 取得总列数
        if(!$highestRow || !$highestColumn){
            return false;
        }
        // 根据自己的数据表的大小修改
         $arr = array(1=>'A',2=>'B',3=>'C',4=>'D',5=>'E',6=>'F',7=>'G',8=>'H',9=>'I',10=>'J',11=>'K',12=>'L',13=>'M', 14=>'N',15=>'O',16=>'P',17=>'Q',18=>'R',19=>'S',20=>'T',21=>'U',22=>'V',23=>'W',24=>'X',25=>'Y',26=>'Z');
         
         // 每次读取一行，再在行中循环每列的数值
         for ($row = 1; $row <= $highestRow; $row++) {
            for ($column = 0; $arr[$column] != 'D'; $column++) {
                $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
                $list[$row][] = nl2br($val);
            }
         }
         //echo "<pre>".var_dump(($list))."</pre>";
         $paperID = $departmentID = 0;
         if(empty($list)) return false;
         foreach ($list as $key => $value) {
             if($key == 2)
             {
                //tr_test_paper 试卷
                $test_paper = [
                                'paperTitle'=>$value[2],
                                'departmentID'=>$value[3],
                                'userID'=>$_SESSION['adminUserInfo']['id'],
                                'createTime'=>date("Y-m-d H:i:s",time())
                                ];
                $paperID = $this->test->createPater($test_paper);
                if(!$paperID) return false;
                $departmentID = $value[3];
             }
             elseif ($key >=4 ) 
             {
                //插入题目
                $questionType = $value[1];
                if(in_array($questionType,[1,2,3,4,5]))
                {
                     $result = $this->questionAnalyse($paperID, $departmentID,$value);
                }
             }
         }
         return $result;
    }

   /**
    * 题目导入分析
    * @return [type] [description]
    */
   public function questionAnalyse($paperID = 0, $departmentID = 0,$question = [])
   {
        //题目tr_test_question
        $tr_test_question_data = [
            'paperID'=>$paperID,
            'questionTile'=>$question[2],
            'questionType'=>$question[1],
            'departmentID'=>$departmentID,
            'CreateTime'=>date("Y-m-d H:i:s", time())
        ];
        $result = $this->test->insert_test_question($tr_test_question_data,$question);
        return $result;
   }

}
/* End of file index.php */
/* Location: ./application/controllers/luckydog/index.php */
